package com.mcml.space;

import java.util.List;
//import java.util.logging.Logger; -Debug only

import org.bukkit.inventory.ItemStack;

public class ItemLores {
    public static String MotaRPGNameLore;
    public static String PhysicsAttackLore;
    public static String PhysicsProtectLore;
    public static String MagicAttackLore;
    public static String MagicProtectLore;
    public static String CombieLore;
    public static String CritLore;
    public static String StrongLore;

    public static boolean IsStrong(ItemStack item) {
        if (item.hasItemMeta() && item.getItemMeta().getLore() != null) {
            List<String> lores = item.getItemMeta().getLore();
            int ls = lores.size();
            for (String s : lores) {
                String lore = s;
                if (lore.contains(StrongLore)) {
                    return true;
                }
            }
        }
        return false;
    }

    public static int getCombieTimes(ItemStack item) {
        if (item.hasItemMeta() && item.getItemMeta().getLore() != null) {
            List<String> lores = item.getItemMeta().getLore();
            int ls = lores.size();
            int times = 1;
            for (String s : lores) {
                String lore = s;
                if (lore.contains(CombieLore)) {
                    lore = removeColorCode(lore);
                    times += toInt(lore.substring(CombieLore.length()));
                }
            }
            return times;
        }
        return 1;
    }

    public static int getCritChance(ItemStack item) {
        if (item.hasItemMeta() && item.getItemMeta().getLore() != null) {
            List<String> lores = item.getItemMeta().getLore();
            int ls = lores.size();
            int times = 0;
            for (String s : lores) {
                String lore = s;
                if (lore.contains(CritLore)) {
                    lore = removeColorCode(lore);
                    times += toInt(lore.substring(CritLore.length()));
                }
            }
            return times;
        }
        return 1;
    }

    public static int getPhysicsAttack(ItemStack item) {
        if (item.hasItemMeta() && item.getItemMeta().getLore() != null) {
            List<String> lores = item.getItemMeta().getLore();
            int ls = lores.size();
            int PA = 0;
            for (String s : lores) {
                String lore = s;
                if (lore.contains(PhysicsAttackLore)) {
                    lore = removeColorCode(lore);
                    PA += toInt(lore.substring(PhysicsAttackLore.length()));
                }
            }
            //Logger.getLogger("debug").info(String.valueOf(PA));
            return PA;
        }
        return 0;
    }

    public static int getMagicAttack(ItemStack item) {
        if (item.hasItemMeta() && item.getItemMeta().getLore() != null) {
            List<String> lores = item.getItemMeta().getLore();
            int ls = lores.size();
            int MA = 0;
            for (String s : lores) {
                String lore = s;
                if (lore.contains(MagicAttackLore)) {
                    lore = removeColorCode(lore);
                    MA += toInt(lore.substring(MagicAttackLore.length()));
                }
            }
            return MA;
        }
        return 0;
    }

    public static int getPhysicsProtect(ItemStack item) {
        if (item.hasItemMeta() && item.getItemMeta().getLore() != null) {
            List<String> lores = item.getItemMeta().getLore();
            int ls = lores.size();
            int PP = 0;
            for (String s : lores) {
                String lore = s;
                if (lore.contains(PhysicsProtectLore)) {
                    lore = removeColorCode(lore);
                    PP += toInt(lore.substring(PhysicsProtectLore.length()));
                }
            }
            return PP;
        }
        return 0;
    }

    public static int getMagicProtect(ItemStack item) {
        if (item.hasItemMeta() && item.getItemMeta().getLore() != null) {
            List<String> lores = item.getItemMeta().getLore();
            int ls = lores.size();
            int MP = 0;
            for (String s : lores) {
                String lore = s;
                if (lore.contains(MagicProtectLore)) {
                    lore = removeColorCode(lore);
                    MP += toInt(lore.substring(MagicProtectLore.length()));
                }
            }
            return MP;
        }

        return 0;
    }

    public static boolean isMotaRPGItem(ItemStack item) {
        if (item != null && item.hasItemMeta() && item.getItemMeta().getLore() != null) {
            List<String> lores = item.getItemMeta().getLore();
            int ls = lores.size();
            for (String s : lores) {
                String lore = s;
                if (lore.contains(MotaRPGNameLore)) {
                    return true;
                }
            }
        }

        return false;
    }

    public static String removeColorCode(String string) {
        string = string.replaceAll("§4", "");
        string = string.replaceAll("§9", "");
        string = string.replaceAll("§c", "");
        string = string.replaceAll("§d", "");
        string = string.replaceAll("§6", "");
        string = string.replaceAll("§5", "");
        string = string.replaceAll("§e", "");
        string = string.replaceAll("§f", "");
        string = string.replaceAll("§2", "");
        string = string.replaceAll("§7", "");
        string = string.replaceAll("§a", "");
        string = string.replaceAll("§8", "");
        string = string.replaceAll("§b", "");
        string = string.replaceAll("§0", "");
        string = string.replaceAll("§3", "");
        string = string.replaceAll("§1", "");
        string = string.replaceAll("§l", "");
        string = string.replaceAll("§k", "");
        string = string.replaceAll("§n", "");
        string = string.replaceAll("§m", "");
        string = string.replaceAll("§o", "");
        string = string.replaceAll("§r", "");
        string = string.replaceAll("%", "");
        return string;
    }

    private static int toInt(String string) {
        int ret;
        ret = Integer.parseInt(string.replaceAll(" ", ""));
        return (ret);
    }

    static {
        MotaRPGNameLore = MotaRPG.ItemLoreMotaRPGName;
        PhysicsAttackLore = MotaRPG.ItemLorePhysicsAttack;
        PhysicsProtectLore = MotaRPG.ItemLorePhysicsProtect;
        MagicAttackLore = MotaRPG.ItemLoreMagicAttack;
        MagicProtectLore = MotaRPG.ItemLoreMagicProtect;
        CombieLore = MotaRPG.ItemLoreCombie;
        CritLore = MotaRPG.ItemLoreCrit;
        StrongLore = MotaRPG.ItemLoreStrong;
    }
}